import type { ElForm } from 'element-plus';
import { getCurrentInstance, ComponentInternalInstance, ref, Ref, reactive, watch } from 'vue';

/**
 * 添加、编辑配置对话框
 */
function useSaveDialog() {
    const formRefKey = 'saveDialogForm';

    const visible = ref(false);
    const saveType: Ref<'add' | 'edit'> = ref('add');

    const callbackMap = {
        confirm: () => {},
    };

    // 当前组件实例
    const current = getCurrentInstance() as ComponentInternalInstance;

    const formData = reactive({
        cate: '',
        content: '',
        description: '',
        cwd: '',
        async: false,
        script: false,
        command: false,
        immediate: false,
    });
    const formRule = {
        cate: [{ required: true, message: '请选择分类', trigger: 'change' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        description: [{ required: true, message: '请输入描述', trigger: 'blur' }],
    };

    // 重置表单
    const initFormData = () => {
        formData.cate = '';
        formData.content = '';
        formData.description = '';
        formData.cwd = '';
        formData.async = false;
        formData.script = false;
        formData.command = false;
        formData.immediate = false;
    };

    const setConfirmCB = (callback: () => void) => {
        callbackMap.confirm = callback;
    };

    const open = (type: 'add' | 'edit' = 'add') => {
        saveType.value = type;
        visible.value = true;
    };

    const close = () => {
        initFormData();
        (current.refs[formRefKey] as typeof ElForm).clearValidate();
        visible.value = false;
    };

    const confirm = () => {
        callbackMap.confirm();
    };

    watch(
        () => formData.script,
        (newVal, oldVal) => {
            if (newVal !== oldVal && newVal) formData.command = false;
        },
    );
    watch(
        () => formData.command,
        (newVal, oldVal) => {
            if (newVal !== oldVal && newVal) formData.script = false;
        },
    );
    watch(formData, newData => {
        if (!newData.command && !newData.script) {
            formData.async = false;
            formData.immediate = false;
        }
    });

    return {
        formData,
        formRule,
        formRefKey,

        visible,
        saveType,

        open,
        close,
        confirm,
        setConfirmCB,
    };
}

/**
 * 执行脚本或命令对话框
 */
function useRunDialog() {
    const formRefKey = 'runDialogForm';

    const visible = ref(false);

    const callbackMap = {
        confirm: () => {},
    };

    // 当前组件实例
    const current = getCurrentInstance() as ComponentInternalInstance;

    const formData = reactive({
        cwd: '',
        content: '',
    });

    // 重置表单
    const initFormData = () => {
        formData.cwd = '';
        formData.content = '';
    };

    const setConfirmCB = (callback: () => void) => {
        callbackMap.confirm = callback;
    };

    const open = () => {
        visible.value = true;
    };

    const close = () => {
        initFormData();
        (current.refs[formRefKey] as typeof ElForm).clearValidate();
        visible.value = false;
    };

    const confirm = () => {
        callbackMap.confirm();
    };

    return {
        formData,
        formRefKey,

        visible,

        open,
        close,
        confirm,
        setConfirmCB,
    };
}

/**
 * 导出数据对话框
 */
function useExportDataDialog() {
    const formRefKey = 'exportDataDialogForm';

    const visible = ref(false);

    const callbackMap = {
        confirm: () => {},
    };

    // 当前组件实例
    const current = getCurrentInstance() as ComponentInternalInstance;

    const formData = reactive({
        cwd: true,
    });

    // 重置表单
    const initFormData = () => {
        formData.cwd = true;
    };

    const setConfirmCB = (callback: () => void) => {
        callbackMap.confirm = callback;
    };

    const open = () => {
        visible.value = true;
    };

    const close = () => {
        initFormData();
        (current.refs[formRefKey] as typeof ElForm).clearValidate();
        visible.value = false;
    };

    const confirm = () => {
        callbackMap.confirm();
    };

    return {
        formData,
        formRefKey,

        visible,

        open,
        close,
        confirm,
        setConfirmCB,
    };
}

export { useSaveDialog, useRunDialog, useExportDataDialog };
